//
//  TEHTTPRequestTest.m
//  TintExample
//
//  Created by Kevin Wu on 9/3/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEHTTPRequestTest.h"

@implementation TEHTTPRequestTest

TTSynthesizeSingleton(TEHTTPRequestTest);



#pragma mark - Accessors

@synthesize loadingList = _loadingList;

@synthesize added = _added;
@synthesize removed = _removed;


#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
        
        _urls = [[NSMutableArray alloc] init];
        [_urls addObject:@"http://tieba.baidu.com/index.html"];
        [_urls addObject:@"http://info.xcar.com.cn/201209/news_634101_1.html"];
        [_urls addObject:@"http://www.tianshannet.com/news/content/2012-09/02/content_7196544.htm"];
        [_urls addObject:@"http://www.tianshannet.com/news/content/2012-09/03/content_7199414.htm"];
        [_urls addObject:@"http://www.sootoo.com/content/336183.shtml"];
        
        [_urls addObject:@"http://www.hbqnb.com/news/index.html"];
        [_urls addObject:@"http://www.bjnews.com.cn/opinion/2012/09/03/220468.html"];
        [_urls addObject:@"http://news.163.com/12/0903/02/8AEOF24200014AED.html"];
        [_urls addObject:@"http://v.163.com/special/jishisubject/historyoftoday115.html"];
        [_urls addObject:@"http://newspaper.dbw.cn/hljcb/html/2011-05/25/node_25.htm"];
        
        
        _loadingList = [[NSMutableArray alloc] init];
        
        _executeLock = [[NSLock alloc] init];
        
        
        _added = 0;
        _removed = 0;
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_urls);
    
    TTRelease(_loadingList);
    
    TTRelease(_executeLock);
    
    _added = 0;
    _removed = 0;
    
    [super dealloc];
}

#pragma mark - Public


- (void)create1:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            [_executeLock lock];
            
            [self cleanRequest];
            [self addRequest];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            [_executeLock unlock];
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)create2:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            [_executeLock lock];
            
            [self cleanRequest];
            [self addRequest];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            [_executeLock unlock];
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)create3:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            [_executeLock lock];
            
            [self cleanRequest];
            [self addRequest];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            [_executeLock unlock];
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)create4:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            [_executeLock lock];
            
            [self cleanRequest];
            [self addRequest];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            [_executeLock unlock];
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)create5:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            
            [_executeLock lock];
            
            [self cleanRequest];
            [self addRequest];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
            
            [_executeLock unlock];
        }
        
        //[NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}


- (void)cleanRequest
{
    NSMutableArray *tmp = [[NSMutableArray alloc] init];
    for ( int i=0; i<[_loadingList count]; ++i ) {
        TTHTTPRequest *request = [_loadingList objectAtIndex:i];
        if ( [request isFinished] ) {
            [tmp addObject:request];
        }
    }
    [_loadingList removeObjectsInArray:tmp];
    _removed += ([tmp count]);
    [tmp release];
}

- (void)addRequest
{
    if ( _added < 5000 ) {
        _added++;
        NSString *url = [_urls objectAtIndex:arc4random()%[_urls count]];
        TTHTTPRequest *request = [[TTHTTPRequest alloc] initWithAddress:url];
        [_loadingList addObject:request];
        [request startAsynchronous];
        [request release];
    }
    
}


#pragma mark - Override


- (void)startTest
{
    [super startTest];
    
    if ( !_launched ) {
        _launched = YES;
        [self performSelectorInBackground:@selector(create1:) withObject:nil];
        [self performSelectorInBackground:@selector(create2:) withObject:nil];
        [self performSelectorInBackground:@selector(create3:) withObject:nil];
        [self performSelectorInBackground:@selector(create4:) withObject:nil];
        [self performSelectorInBackground:@selector(create5:) withObject:nil];
    }
}


@end
